package com.openingedu.mba.service;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.util.Log;

import com.j256.ormlite.dao.ForeignCollection;
import com.j256.ormlite.stmt.QueryBuilder;
import com.j256.ormlite.stmt.Where;
import com.openingedu.mba.entity.QuestionVO;
import com.openingedu.mba.persist.DataHelper;
import com.openingedu.mba.persist.DbFaultQuestion;
import com.openingedu.mba.persist.DbHistory;
import com.openingedu.mba.persist.DbPaper;
import com.openingedu.mba.persist.DbPaperQuestion;
import com.openingedu.mba.persist.DbQuestion;

public class PracticeService {

	private static final String TAG = PracticeService.class.getSimpleName();

	public synchronized void persistHistory(Context context, DbHistory history) {
		DataHelper dataHelper = new DataHelper(context);
		try {
			dataHelper.getHistoryDao().create(history);
			Log.i(TAG, "persist practice history success");
		} catch (SQLException e) {
			e.printStackTrace();
			Log.e(TAG, "persist practice history error." + e.getMessage(), e);
		}
	}

	public List<DbHistory> listHistory(Context context) {
		DataHelper dataHelper = new DataHelper(context);
		List<DbHistory> list = new ArrayList<DbHistory>();
		try {
			QueryBuilder<DbHistory, Long> queryBuilder = dataHelper.getHistoryDao().queryBuilder();
			queryBuilder.orderBy("practiceDate", false);
			list = queryBuilder.query();
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}
			
	/**
	 * 保存错误问题
	 * @throws SQLException 
	 */
	public void saveFaultQuestion(Context context, DataHelper dataHelper, QuestionVO question) throws SQLException {
		DbFaultQuestion faultQuestion = findFaultQuestion(context, dataHelper, question.getId());
		if (faultQuestion != null) {
			faultQuestion.setQuestionID(question.getId());
			faultQuestion.setFaultCount(faultQuestion.getFaultCount() + 1);
			dataHelper.getDbFaultQuestionDao().update(faultQuestion);
		}
		else {
			faultQuestion = new DbFaultQuestion();
			faultQuestion.setQuestionID(question.getId());
			faultQuestion.setFaultCount(0);
			dataHelper.getDbFaultQuestionDao().create(faultQuestion);
		}
	}

	private DbFaultQuestion findFaultQuestion(Context context, DataHelper dataHelper, Integer questionID) {
		DbFaultQuestion faultQuestion = null;
		try {
			List<DbFaultQuestion> faultQuestionList = dataHelper.getDbFaultQuestionDao().queryForEq("questionID", questionID);
			if (faultQuestionList != null && faultQuestionList.size()>0) {
				faultQuestion = faultQuestionList.get(0);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return faultQuestion;
	}

	public List<DbFaultQuestion> listFaultQuestion(Context context) throws SQLException {
		DataHelper dataHelper = new DataHelper(context);
		return dataHelper.getDbFaultQuestionDao().queryForAll();
	}
	
	public List<DbQuestion> listFaultDbQuestion(Context context) throws SQLException {
		DataHelper dataHelper = new DataHelper(context);
		List<DbFaultQuestion> faultQuestionList = listFaultQuestion(context);
		List<Integer> questionIDList = new ArrayList<Integer>();
		for (DbFaultQuestion faultQuestion : faultQuestionList) {
			questionIDList.add(faultQuestion.getQuestionID());
		}
		QueryBuilder<DbQuestion, Long> queryBuilder = dataHelper.getDbQuestionDao().queryBuilder();
		Where<DbQuestion, Long> where = queryBuilder.where();
		where.in("questionID", questionIDList);
		return queryBuilder.query();
	}

	public List<DbQuestion> loadPaperQuestion(Context context, Integer paperID) {
		DataHelper dataHelper = new DataHelper(context);
		try {
			List<DbPaper> paperList = dataHelper.getDbPaperDao().queryForEq("paperID", paperID);
			if (paperList!=null && paperList.size() > 0) {
				DbPaper paper = paperList.get(0);
				ForeignCollection<DbPaperQuestion> paperQuestion = paper.getPaperQuestions();
				List<Integer> questionIDList = new ArrayList<Integer>();
				for (DbPaperQuestion dbPaperQuestion : paperQuestion) {
					questionIDList.add(dbPaperQuestion.getQuestionID());
					Log.i(TAG, "add questionID=" + dbPaperQuestion.getQuestionID() + ", ID=" + dbPaperQuestion.getId());
				}
				QueryBuilder<DbQuestion, Long> queryBuilder = dataHelper.getDbQuestionDao().queryBuilder();
				Where<DbQuestion, Long> where = queryBuilder.where();
				where.in("questionID", questionIDList);
				return queryBuilder.query();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
}
